package com.manor.paradise.activity

import android.content.Intent
import android.widget.ImageSwitcher
import androidx.viewpager.widget.ViewPager
import com.manor.common.Constants
import com.manor.common.activity.AbsActivity
import com.manor.common.utils.DrawableUtil
import com.manor.common.utils.KLog
import com.manor.paradise.R
import com.manor.paradise.adapter.CommonPagerAdapter
import com.manor.paradise.bean.AddressBean
import com.manor.paradise.fragment.npcshop.ExchangeGiftsFragment
import com.manor.paradise.fragment.npcshop.ExchangeNobilityFragment
import kotlinx.android.synthetic.main.activity_npc_shop.*

/**
 * 淘米商店
 */
class NpcShopActivity : AbsActivity() {
    private lateinit var exchangeGiftsFragment: ExchangeGiftsFragment
    internal  var tv_list: ArrayList<ImageSwitcher>  = ArrayList()
    private var older_int = 0
    override fun getLayoutId(): Int {
        return R.layout.activity_npc_shop
    }

    override fun main() {
        super.main()
        tv_list.add(tv_gifts)
        tv_list.add(tv_nobility)
        tv_list.add(tv_props)

        tv_gifts.background = DrawableUtil.getDrawable(16, "#FF137EF3")
        tv_nobility.background = DrawableUtil.getDrawable(16, "#FF1C1C1C")
        tv_props.background = DrawableUtil.getDrawable(16, "#FF1C1C1C")

        tv_gifts.setInAnimation(this, android.R.anim.fade_in)
        tv_gifts.setOutAnimation(this, android.R.anim.fade_out)
        tv_nobility.setInAnimation(this, android.R.anim.fade_in)
        tv_nobility.setOutAnimation(this, android.R.anim.fade_out)
        tv_props.setInAnimation(this, android.R.anim.fade_in)
        tv_props.setOutAnimation(this, android.R.anim.fade_out)
         exchangeGiftsFragment = ExchangeGiftsFragment.newInstance()
        val pagerAdapter = CommonPagerAdapter(supportFragmentManager, exchangeGiftsFragment, ExchangeNobilityFragment(mContext))//, ExchangePropsFragment(mContext)
        viewpager.offscreenPageLimit = tv_list.size
        viewpager.adapter = pagerAdapter
        viewpager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                setViewpagershow(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
                KLog.e("tag", state.toString() + "")
            }
        })
        im_back.setOnClickListener { finish() }
        tv_gifts.setOnClickListener {
            viewpager.currentItem = 0
        }
        tv_nobility.setOnClickListener {
            viewpager.currentItem = 1
        }
        tv_props.setOnClickListener {
            viewpager.currentItem = 2
        }
    }
    private fun setViewpagershow(tv: Int) {
        tv_list.get(tv).background = DrawableUtil.getDrawable(16, "#FF137EF3")
        tv_list.get(older_int).background = DrawableUtil.getDrawable(16, "#FF1C1C1C")
        older_int = tv


    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null != data) {
            if (resultCode == 102) {
                if(null!=data.getSerializableExtra(Constants.GIFT_CITY)) {
                    exchangeGiftsFragment.run {
                        exchangeGiftsFragment.setGiftPop(data.getSerializableExtra(Constants.GIFT_CITY) as AddressBean.RecordsBean)
                    }
                }

            }
        }
    }
}